<template>
  <div class="table-box">
    <div class="table-search">
      <div class="btn-box">
        <el-button type="primary" @click="onAdd">新增</el-button>
        <el-button type="primary" plain @click="onExport">导出</el-button>
      </div>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="ID" align="center" />
          <el-table-column prop="name" label="任务名字" align="center" />
          <el-table-column prop="description" label="任务描述" align="center" />
          <el-table-column prop="diamond" label="奖励钻石" align="center" />
          <el-table-column
            prop="go_name"
            label="跳转app页面名字"
            align="center"
          />
          <el-table-column prop="go_type" label="跳转类型" align="center" />
          <el-table-column prop="parameter" label="跳转参数" align="center" />
          <el-table-column
            prop="createtime"
            label="时间戳"
            align="center"
            width="180"
            :formatter="formatterTime"
          />

          <el-table-column
            label="操作"
            align="center"
            fixed="right"
            width="200"
          >
            <template #default="scope">
              <el-button
                type="primary"
                plain
                @click="handleOperate(scope.$index, scope.row, 'edit')"
                >更新</el-button
              >
              <el-button
                type="danger"
                @click="handleOperate(scope.$index, scope.row, 'delete')"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
    <DialogForm
      @cancel="cancel"
      @closeDialog="closeDialog"
      @submitForm="submit"
      :dialogFormVisible="dialogVisible"
      :dialogForm="dialogForm"
      :btnLoading="btnLoading"
      :bannerPageList="bannerPageList"
    />
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import { exportToExcel, formatterTime } from "@/utils/utils";
import {
  taskListApi,
  taskDelApi,
  taskAddApi,
  taskUpdateApi,
} from "@/api/slotsgame/task/task";
import { bannerPageListApi } from "@/api/slotsgame/game/game";
import DialogForm from "./components/dialogForm.vue";
const loading = ref(false);
const tableData = ref([]);
const btnLoading = ref(false);
const dialogVisible = ref(false);
const btnType = ref(null);
const bannerPageList = ref([]);
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});
const dialogFormData = {
  id: "",
  name: "",
  description: "",
  diamond: "",
  go_name: "",
  go_type: "",
  url: "",
};
const dialogForm = reactive({ ...dialogFormData });
// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
  };
};

// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;

  try {
    const res = await taskListApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};
const getbannerPageData = async () => {
  loading.value = true;

  try {
    const res = await bannerPageListApi({});
    bannerPageList.value = res.data;
    loading.value = false;
  } catch (error) {
    loading.value = false;
  }
};

// 查询
const onAdd = async () => {
  dialogVisible.value = true;
  btnType.value = "add";
};
// 导出
const onExport = async () => {
  const data = Object.assign(param(), { export: 1 });
  const res = await taskListApi(data);

  let head = {
    id: "ID",
    name: "任务名字",
    description: "任务描述",
    diamond: "奖励钻石",
    go_name: "跳转app页面名字",
    go_type: "跳转类型",
    parameter: "跳转参数",
    createtime: "时间戳",
  };
  exportToExcel(res.data.results, head, "Slotsgame-任务列表");
};
//关闭
const closeDialog = () => {
  dialogVisible.value = false;
  btnType.value = null;
  Object.assign(dialogForm, dialogFormData);
};

// 取消
const cancel = () => {
  dialogVisible.value = false;
  btnType.value = null;
  Object.assign(dialogForm, dialogFormData);
};
// 提交
const submit = async (param) => {
  try {
    btnLoading.value = true;
    if (btnType.value == "add") {
      delete param.id;
      delete param.parameter;
      delete param.createtime;
      const res = await taskAddApi(param);
      ElMessage({
        message: res.message,
        type: "success",
      });
    }
    if (btnType.value == "edit") {
      const res = await taskUpdateApi(param);
      ElMessage({
        message: res.message,
        type: "success",
      });
    }
    dialogVisible.value = false;
    btnLoading.value = false;
    getTbaleData();
  } catch (error) {
    btnLoading.value = false;
    dialogVisible.value = false;
  }
};
const handleOperate = async (index, row, type) => {
  // 编辑
  if (type === "edit") {
    btnType.value = "edit";
    dialogVisible.value = true;
    Object.assign(dialogForm, { ...row });
  }
  // 删除
  if (type === "delete") {
    ElMessageBox.confirm("您是否确认删除此任务?", "温馨提示", {
      type: "warning",
    }).then(async () => {
      const res = await taskDelApi({ id: row.id });
      getTbaleData();
      ElMessage.success(res.message);
    });
  }
};
// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};
// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
  getbannerPageData();
});
</script>
